// Tree: 平级数据 通过 childKey parentKey childArray 组装为有层级关系的数据
export const transformToTreeFormat = function (array, cKey, pKey, cArray) {
  // 子节点childKey  父节点parentKey
  let i, l, childKey = cKey, parentKey = pKey, childArray = cArray
  if (array instanceof Array) {
    let r = []
    let tmpMap = {}
    for (i = 0, l = array.length; i < l; i++) {
      if (array[i]) {
        tmpMap[array[i][childKey]] = array[i]
      }
    }
    for (i = 0, l = array.length; i < l; i++) {
      if (array[i] && !!tmpMap[array[i][parentKey]] && array[i][childKey] !== array[i][parentKey]) {
        if (!tmpMap[array[i][parentKey]][childArray]) {
          tmpMap[array[i][parentKey]][childArray] = []
        }
        tmpMap[array[i][parentKey]][childArray].push(array[i])
      } else {
        r.push(array[i])
      }
    }
    return r
  } else {
    return [array]
  }
}
